/**
 * 
 */
package com.lanswon.qzsmk.service.systemManageService.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lanswon.qzsmk.base.DwzPage;
import com.lanswon.qzsmk.base.HttpContants;
import com.lanswon.qzsmk.base.SystemManageContants;
import com.lanswon.qzsmk.dao.AddressStreetMapper;
import com.lanswon.qzsmk.dao.BranchMapper;
import com.lanswon.qzsmk.dao.OrgMapper;
import com.lanswon.qzsmk.exception.ApplicationException;
import com.lanswon.qzsmk.exception.ErrorCode;
import com.lanswon.qzsmk.model.AddressStreet;
import com.lanswon.qzsmk.model.Branch;
import com.lanswon.qzsmk.model.Org;
import com.lanswon.qzsmk.service.systemManageService.BranchManageService;
import com.lanswon.qzsmk.util.CopyBean;
import com.lanswon.qzsmk.vo.BranchInternetVo;
import com.lanswon.qzsmk.vo.BranchVo;
import com.lanswon.qzsmk.vo.OrgCorpVo;
import com.lanswon.qzsmk.vo.OrgVo;
import com.lanswon.qzsmk.vo.ResultObject;
import com.lanswon.qzsmk.vo.ResultStatus;
import com.lanswon.qzsmk.vo.ResultVo;
import com.lanswon.qzsmk.vo.SaveOrUpdateBranchVo;
import com.lanswon.qzsmk.vo.ServerTypeVo;
import com.lanswon.qzsmk.vo.StreetNetVo;

/**
 * @author sun
 *
 */
@Service
@Transactional
public class BranchManageServiceImpl implements BranchManageService {

	@Autowired
	BranchMapper bm;

	@Autowired
	OrgMapper om;
	
	@Autowired
	AddressStreetMapper asm;

	@Override
	public int[] addBranch(SaveOrUpdateBranchVo b) throws ApplicationException {
		Org o = om.queryFirstOrg();
		
		if (o == null) {
			// 网点编号初始值
			b.setOrgNo(10000000);
		} else {
			// 检查是否存在该网点名称
			if(checkIsRepeatByName(b.getOrgName())){
				throw new ApplicationException(ErrorCode.BRANCH_NAME_EXIST, " OrgName ["+b.getOrgName()+"] already exist!!!");
			}
			b.setOrgNo(o.getOrgNo() + 1);
		}
		om.insert(b);
		b.setBranchId(b.getOrgId());
		bm.insert(b);
		return new int[]{b.getOrgId(),b.getOrgNo()};
	}

	@Override
	public void modifyBranch(SaveOrUpdateBranchVo b) {
		om.update(b);
		b.setBranchId(om.queryOrgByNo(b.getOrgNo()).getOrgId());
		bm.update(b);		

	}

	@Override
	public DwzPage<Org> queryBranchByName(BranchVo t) {
		
		List<Org> list = om.queryOrgByName(t);

		int totalCount = om.queryOrgCountByName(t);

		DwzPage<Org> dp = new DwzPage<Org>(t.getCurrentPage(), t.getNumPerPage(), totalCount);

		dp.setList(list);
		
		
		return dp;

	}

	@Override
	public SaveOrUpdateBranchVo queryBranchByNo(int branchNo) throws Exception {
		if (branchNo == 0) {
			throw new ApplicationException(ErrorCode.OBJECT_IS_NULL, "branchId is null!!!");
		}
		SaveOrUpdateBranchVo vo = new SaveOrUpdateBranchVo();
		Org o = om.queryOrgByNo(branchNo);
		Branch b = bm.queryBranchById(o.getOrgId());
		vo = (SaveOrUpdateBranchVo) CopyBean.copyA2b(o, vo);
		
		vo = (SaveOrUpdateBranchVo) CopyBean.copyA2b(b, vo);
		return vo;
	}
	
	
	@Override
	public ResultVo queryOrgByName(String branchName){

		ResultVo rv = new ResultVo();
		ResultStatus rs =new ResultStatus();
		
		rs.setCode(HttpContants.RETURN_SUCCESS);
		rs.setMessage("返回成功");
		rv.setResultStatus(rs);
		if(StringUtils.isEmpty(branchName)){
			rs.setCode(ErrorCode.OBJECT_IS_NULL);
			rs.setMessage("网点名称不能为空");
			rv.setResultStatus(rs);
			
			return rv;
		}
		
		
		Org o = om.queryOrgByOrgName(branchName);
		BranchInternetVo v = new BranchInternetVo();
		v.setAddress(o.getAddress());
		v.setBranchName(o.getOrgName());
		v.setDate(o.getServerDate());
		v.setServiceInfo("市民卡补换卡,充值、申请、挂失/解挂、注销");
		v.setTelPhone(o.getTelephone());
		
		rv.setServiceObject(v);
		
		return rv;
	}

	@Override
	public ResultVo queryBranch(String streetId,String orgProperty) {
		
		ResultVo rv = new ResultVo();
		ResultStatus rs =new ResultStatus();
		
		rs.setCode(HttpContants.RETURN_SUCCESS);
		rs.setMessage("返回成功");
		
		rv.setResultStatus(rs);
		
		
		ResultObject ro = new ResultObject();
		List<Org> list = om.queryOrg(streetId, orgProperty,SystemManageContants.STATUS_ENABLE);
		
		ro.setItems(list);
		rv.setServiceObject(ro);
		return rv;
	}
	
	@Override
	public ResultVo queryBranchInternet(String streetId,String orgProperty,String serverType) {
		
		ResultVo rv = new ResultVo();
		ResultStatus rs =new ResultStatus();
		
		rs.setCode(HttpContants.RETURN_SUCCESS);
		rs.setMessage("返回成功");
		
		rv.setResultStatus(rs);
		
		
		ResultObject ro = new ResultObject();
		List<OrgVo> list = om.queryOrg_s(streetId, orgProperty,SystemManageContants.STATUS_ENABLE,serverType);
		Map<String,String> map = SystemManageContants.map;
		for(OrgVo ov:list){
			ov.setCorpName(map.get(ov.getInnerNo()));
			ov.setServiceTypeName(SystemManageContants.getServerTypeByCode(ov.getServiceType()));
		}
		ro.setItems(list);
		rv.setServiceObject(ro);
		return rv;
	}
	
	@Override
	public Org queryOrgByNo(int orgNo){
		
		return om.queryOrgByNo(orgNo);
	}

	@Override
	public boolean checkIsRepeatByName(String orgName) throws ApplicationException{
		if(StringUtils.isEmpty(orgName)){
			throw new ApplicationException(ErrorCode.OBJECT_IS_NULL, "orgName is null!!!");
		}
		if(om.queryCountByName(orgName)>0){
			return true;
		}else{
			return false;
		}
		
	}
	

	private static List<StreetNetVo> getDuplicate1(List<StreetNetVo> list) {
		for (int i = 0; i < list.size() - 1; i++) {
			for (int j = list.size() - 1; j > i; j--) {
				if (list.get(j).equals(list.get(i))){
					list.remove(i);
				}
			}
		}
		return list;
	}
	
	@Override
	public ResultVo queryAllBranchStreet() {
		ResultVo rv = new ResultVo();
		ResultStatus rs =new ResultStatus();
		List<AddressStreet> list= asm.queryAddressStreetByFatherId(Long.valueOf(SystemManageContants.cityCode));
		List<StreetNetVo> ls = new ArrayList<>();
		if(list.size() == 0){
			rs.setCode(HttpContants.RETURN_ERROR);
			rs.setMessage("查询街道异常");
			rv.setResultStatus(rs);

			return rv;
		}
		for(AddressStreet as:list){
			StreetNetVo vvv= new StreetNetVo(String.valueOf(as.getStreetid()), as.getStreet());
			ls.add(vvv);
		}
		
		rs.setCode(HttpContants.RETURN_SUCCESS);
		rs.setMessage("返回成功");
		rv.setResultStatus(rs);
		
		
		ResultObject ro = new ResultObject();
		
		ro.setItems(ls);
		rv.setServiceObject(ro);
		return rv;
	}
	
	@Override
	public ResultVo queryOrgProperty() {
		List<StreetNetVo> ls = new ArrayList<>();
		StreetNetVo vvv= new StreetNetVo(SystemManageContants.ORG_PROPERTY_GOV, SystemManageContants.getOrgPropertyNameByCode(SystemManageContants.ORG_PROPERTY_GOV));
		ls.add(vvv);
		 vvv= new StreetNetVo(SystemManageContants.ORG_PROPERTY_STATE_OWNED_ENTERPRISE, SystemManageContants.getOrgPropertyNameByCode(SystemManageContants.ORG_PROPERTY_STATE_OWNED_ENTERPRISE));
		ls.add(vvv);
		 vvv= new StreetNetVo(SystemManageContants.ORG_PROPERTY_COMPANY, SystemManageContants.getOrgPropertyNameByCode(SystemManageContants.ORG_PROPERTY_COMPANY));
		ls.add(vvv);
		 vvv= new StreetNetVo(SystemManageContants.ORG_PROPERTY_INDIVIDUAL, SystemManageContants.getOrgPropertyNameByCode(SystemManageContants.ORG_PROPERTY_INDIVIDUAL));
			ls.add(vvv);
		
		ResultVo rv = new ResultVo();
		ResultStatus rs =new ResultStatus();
		
		rs.setCode(HttpContants.RETURN_SUCCESS);
		rs.setMessage("返回成功");
		
		rv.setResultStatus(rs);
		
		
		ResultObject ro = new ResultObject();
		ro.setItems(ls);
		rv.setServiceObject(ro);
		return rv;
	}
	
	@Override
	public ResultVo queryOrgServerType() {
		List<ServerTypeVo> ls = new ArrayList<>();
		ServerTypeVo vvv= new ServerTypeVo(SystemManageContants.BRANCH_SERVER_TYPE_RECHARGE, SystemManageContants.getServerTypeByCode(SystemManageContants.BRANCH_SERVER_TYPE_RECHARGE));
		ls.add(vvv);
		 vvv= new ServerTypeVo(SystemManageContants.BRANCH_SERVER_TYPE_CONSUME, SystemManageContants.getServerTypeByCode(SystemManageContants.ORG_PROPERTY_STATE_OWNED_ENTERPRISE));
		ls.add(vvv);
		 vvv= new ServerTypeVo(SystemManageContants.BRANCH_SERVER_TYPE_All, SystemManageContants.getServerTypeByCode(SystemManageContants.ORG_PROPERTY_COMPANY));
		ls.add(vvv);
		
		ResultVo rv = new ResultVo();
		ResultStatus rs =new ResultStatus();
		
		rs.setCode(HttpContants.RETURN_SUCCESS);
		rs.setMessage("返回成功");
		
		rv.setResultStatus(rs);
		
		
		ResultObject ro = new ResultObject();
		ro.setItems(ls);
		rv.setServiceObject(ro);
		return rv;
	}
	
	@Override
	public ResultVo queryOrgCorp() {
		Map<String,String> map = SystemManageContants.map;
		
		List<OrgCorpVo> ls = new ArrayList<>();
		
		for(Entry<String, String> e:map.entrySet()){
			OrgCorpVo vvv= new OrgCorpVo(e.getKey(),e.getValue());
			ls.add(vvv);
		}
		
		ResultVo rv = new ResultVo();
		ResultStatus rs =new ResultStatus();
		
		rs.setCode(HttpContants.RETURN_SUCCESS);
		rs.setMessage("返回成功");
		
		rv.setResultStatus(rs);
		
		
		ResultObject ro = new ResultObject();
		ro.setItems(ls);
		rv.setServiceObject(ro);
		return rv;
		
	}
}
